home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / trackdisk / trackdisk.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  299 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* trackdisk.c */
  31.  
  32. /* program to demonstrate the use of the trackdisk driver */
  33.  
  34. /* author: Rob Peck, 12/1/85 */
  35.  
  36. /* This code may be freely utilized to develop programs for the Amiga */
  37.  
  38.  
  39. #include "exec/types.h"
  40. #include "exec/nodes.h"
  41. #include "exec/lists.h"
  42. #include "exec/memory.h"
  43. #include "exec/interrupts.h"
  44. #include "exec/ports.h"
  45. #include "exec/libraries.h"
  46. #include "exec/io.h"
  47. #include "exec/tasks.h"
  48. #include "exec/execbase.h"
  49. #include "exec/devices.h"
  50. #include "devices/trackdisk.h"
  51.  
  52. #define TD_READ CMD_READ
  53. #define BLOCKSIZE TD_SECTOR
  54.  
  55. SHORT error;
  56. struct MsgPort *diskport;
  57. struct IOExtTD *diskreq;
  58. BYTE diskbuffer[BLOCKSIZE];
  59. BYTE *diskdata;
  60. SHORT testval;
  61.  
  62. extern struct MsgPort *CreatePort();
  63. extern struct IORequest *CreateExtIO();
  64.  
  65. ULONG diskChangeCount;    
  66.         
  67. ReadCylSec(cyl, sec, hd)
  68. SHORT cyl, sec, hd;
  69. {
  70.     LONG offset;
  71.  
  72.     diskreq->iotd_Req.io_Length = BLOCKSIZE;      
  73.     diskreq->iotd_Req.io_Data = (APTR)diskbuffer;    
  74.         /* show where to put the data when read */
  75.     diskreq->iotd_Req.io_Command = ETD_READ;
  76.         /* check that disk not changed before reading */
  77.     diskreq->iotd_Count = diskChangeCount;
  78.     
  79.     /* convert from cylinder, head, sector to byte-offset value to get
  80.         * right one (as dos and everyone else sees it)...*/
  81.     
  82.     /* driver reads one CYLINDER at a time (head does not move for
  83.      * 22 sequential sector reads, or better-put, head doesnt move for
  84.      * 2 sequential full track reads.)
  85.      */
  86.     
  87.     offset = TD_SECTOR * (sec + NUMSECS * hd + NUMSECS * NUMHEADS * cyl);
  88.     diskreq->iotd_Req.io_Offset = offset;
  89.     DoIO(diskreq);
  90.     return(0);
  91. }
  92.  
  93.  
  94. MotorOn()
  95. {
  96.     /* TURN ON DISK MOTOR ... old motor state is returned in io_Actual */
  97.     diskreq->iotd_Req.io_Length = 1;
  98.     /* this says motor is to be turned on */
  99.     diskreq->iotd_Req.io_Command = TD_MOTOR;
  100.     /* do something with the motor */
  101.     DoIO(diskreq);
  102.     printf("\nOld motor state was: %ld",diskreq->iotd_Req.io_Actual);
  103.     printf("\nio_Error value was: %ld",diskreq->iotd_Req.io_Error);
  104.     return(0);
  105. }
  106.  
  107. MotorOff()
  108. {
  109.     printf("\n\nNow turn it off");
  110.     diskreq->iotd_Req.io_Length = 0;    
  111.     /* says that motor is to be turned on */
  112.     diskreq->iotd_Req.io_Command = TD_MOTOR;    
  113.     /* do something with the motor */
  114.     DoIO(diskreq);
  115.     printf("\nOld motor state was: %ld",diskreq->iotd_Req.io_Actual);
  116.     printf("\nio_Error value was: %ld",diskreq->iotd_Req.io_Error);
  117.     return(0);
  118. }
  119.  
  120. SeekFullRange(howmany)
  121. SHORT howmany;
  122. {
  123. int i;
  124. for(i=0; i<howmany; i++)
  125.     {
  126.         diskreq->iotd_Req.io_Offset = 
  127.             ((NUMCYLS -1)*NUMSECS*NUMHEADS -1 ) * 512;
  128.         /* seek to cylinder 79, head 1 */
  129.         diskreq->iotd_Req.io_Command = TD_SEEK;
  130.         DoIO(diskreq);
  131.         if(diskreq->iotd_Req.io_Error != 0) 
  132.             printf("\nSeek Cycle Number %ld, Error = %ld",
  133.                         i, diskreq->iotd_Req.io_Error);
  134.         diskreq->iotd_Req.io_Offset = 0;
  135.             /* seek to cylinder 0, head 0 */
  136.         diskreq->iotd_Req.io_Command = TD_SEEK;
  137.         DoIO(diskreq);
  138.         if(diskreq->iotd_Req.io_Error != 0) 
  139.             printf("\nSeek Cycle Number %ld, Error = %ld",
  140.                         i, diskreq->iotd_Req.io_Error);
  141.         printf("\nCompleted a seek");
  142.     }
  143.     return(0);
  144. }
  145.  
  146.  
  147. main()
  148. {
  149.     SHORT cylinder,head,sector;
  150.     
  151.     diskdata = &diskbuffer[0];    
  152.         /* point to first location in disk buffer */
  153.     diskport = CreatePort(0,0);
  154.     if(diskport == 0) exit(100);    /* error in createport */
  155.     diskreq = (struct IOExtTD *)CreateExtIO(diskport,
  156.                         sizeof(struct IOExtTD));     
  157.         /* make an io request block for communicating with the disk */
  158.     if(diskreq == 0) { DeletePort(diskport); exit(200); }    
  159.  
  160.         error = OpenDevice(TD_NAME,0,diskreq,0);
  161.             /* open the device for access, unit 0 is builtin drive */
  162.         printf("\nError value returned by OpenDevice was: %lx", error);
  163.     
  164.         /* now get the disk change value */
  165.         diskreq->iotd_Req.io_Command = TD_CHANGENUM;
  166.         DoIO(diskreq);
  167.         diskChangeCount = diskreq->iotd_Req.io_Actual;
  168.         printf("\nChange number for disk is currently %ld",diskChangeCount);
  169.     
  170.         MotorOn();
  171.         SeekFullRange(10);
  172.            for(cylinder=0; cylinder<80; cylinder++)    /* tracks to test */
  173.                  {
  174.                  for(head=0; head<2; head++)    /* number of heads to test */
  175.                 for(sector=0; sector<11; sector++)    /* sectors to test */
  176.             {
  177.             ReadCylSec(cylinder, sector, head);
  178.             if(diskreq->iotd_Req.io_Error != 0) 
  179.                printf("\nError At Cyl=%ld, Sc=%ld, Hd=%ld, Error=%ld",
  180.                         cylinder,sector,head,
  181.                     diskreq->iotd_Req.io_Error);
  182.             }
  183.                   printf("\nCompleted reading Cylinder=%ld",cylinder);
  184.                   }
  185.         MotorOff();
  186.         CloseDevice(diskreq);
  187.  
  188.     DeleteExtIO(diskreq, sizeof(struct IOExtTD));
  189.     DeletePort(diskport);
  190. }    /* end of main */
  191.  
  192.  
  193. /***********************************************************************
  194. *
  195. *    Exec Support Function -- Extended IO Request
  196. *
  197. ***********************************************************************/
  198.  
  199. extern APTR AllocMem();
  200.  
  201. /****** exec_support/CreateExtIO **************************************
  202. *
  203. *   NAME    
  204. *    CreateExtIO() -- create an Extended IO request
  205. *
  206. *   SYNOPSIS
  207. *    ioReq = CreateExtIO(ioReplyPort,size);   
  208. *
  209. *   FUNCTION
  210. *    Allocates memory for and initializes a new IO request block
  211. *    of a user-specified number of bytes.
  212. *
  213. *   INPUTS
  214. *    ioReplyPort - a pointer to an already initialized
  215. *        message port to be used for this IO request's reply port.
  216. *
  217. *   RESULT
  218. *    Returns a pointer to the new block.  Pointer is of the type
  219. *    struct IORequest.
  220. *
  221. *    0 indicates inability to allocate enough memory for the request block
  222. *    or not enough signals available.
  223. *
  224. *   EXAMPLE
  225. *    struct IORequest *myBlock;
  226. *    if( (myBlock = CreateExtIO(myPort,sizeof(struct IOExtTD)) == NULL)
  227. *        exit(NO_MEM_OR_SIGNALS);
  228. *
  229. *    example used to allocate space for IOExtTD (trackdisk driver
  230. *    IO Request block for extended IO operations).
  231. *
  232. *   SEE ALSO
  233. *    DeleteExtIO
  234. *
  235. ***********************************************************************/
  236.  
  237. struct IORequest *CreateExtIO(ioReplyPort,size)
  238.     struct MsgPort *ioReplyPort;
  239.     LONG size;
  240. {
  241.     struct IORequest *ioReq;
  242.  
  243.     if (ioReplyPort == 0)
  244.     return ((struct IORequest   *) 0);
  245.  
  246.     ioReq = (struct IORequest *)AllocMem (size, MEMF_CLEAR | MEMF_PUBLIC);
  247.  
  248.     if (ioReq == 0)
  249.     return ((struct IORequest   *) 0);
  250.  
  251.     ioReq -> io_Message.mn_Node.ln_Type = NT_MESSAGE;
  252.     ioReq -> io_Message.mn_Node.ln_Pri = 0;
  253.  
  254.     ioReq -> io_Message.mn_ReplyPort = ioReplyPort;
  255.  
  256.     return (ioReq);
  257. }
  258.  
  259. /****** exec_support/DeleteExtIO **************************************
  260. *
  261. *   NAME
  262. *    DeleteExtIO() - return memory allocated for extended IO request
  263. *
  264. *   SYNOPSIS
  265. *    DeleteExtIO(ioReq,size);
  266. *
  267. *   FUNCTION
  268. *    See summary line at NAME.  Also frees the signal bit which
  269. *    had been allocated by the call to CreateExtIO.
  270. *
  271. *   INPUTS
  272. *    A pointer to the IORequest block whose resources are to be freed.
  273. *
  274. *   RESULT
  275. *    Frees the memory.  Returns (no error conditions shown)
  276. *
  277. *   EXAMPLE
  278. *    struct IORequest *myBlock;
  279. *    DeleteExtIO(myBlock,(sizeof(struct IOExtTD)));
  280. *        
  281. *    example shows that CreateExtIO had been used to create a trackdisk
  282. *    (extended) IO Request block.
  283. *
  284. *   SEE ALSO
  285. *    CreateExtIO
  286. *
  287. **************************************************************************/
  288.  
  289. DeleteExtIO(ioExt,size)
  290.     struct IORequest *ioExt;
  291.     LONG size;
  292. {
  293.     ioExt -> io_Message.mn_Node.ln_Type = 0xff;
  294.     ioExt -> io_Device = (struct Device *) -1;
  295.     ioExt -> io_Unit = (struct Unit *) -1;
  296.  
  297.     FreeMem (ioExt, size);
  298. }
  299.